package com.example.pm.ending.server.service.ec.ownermodule;

import com.example.pm.ending.server.ex.ServiceException;
import com.example.pm.ending.server.mapper.*;
import com.example.pm.ending.server.module.owner.AddNewOwnerModuleDTO;
import com.example.pm.ending.server.pojo.entity.Carport;
import com.example.pm.ending.server.pojo.entity.House;
import com.example.pm.ending.server.pojo.entity.HouseUnit;
import com.example.pm.ending.server.pojo.entity.Owner;
import com.example.pm.ending.server.pojo.vo.*;
import com.example.pm.ending.server.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Calendar;

import static com.example.pm.ending.server.service.IOwnerModuleService.*;

@Component
@Slf4j
public class AddNewOwnerES {

    @Autowired
    public OwnerMapper ownerMapper;
    @Autowired
    public BuildUnitRelMapper buildUnitRelMapper;
    @Autowired
    public HouseBuildMapper houseBuildMapper;
    @Autowired
    public HouseUnitMapper houseUnitMapper;
    @Autowired
    public HouseMapper houseMapper;
    @Autowired
    public CarportAreaMapper carportAreaMapper;
    @Autowired
    public CarportMapper carportMapper;

    public void addNewOwner(AddNewOwnerModuleDTO dto) {
        log.debug("开始处理[添加业主]业务");
        log.debug("传来的参数为：{}", dto);

        //owner表业务处理
        
        Owner owner = ownerService(dto);

        OwnerStandardVO ownerVO = ownerMapper.getOwnerByIdCard(owner.getIdCard());
        log.debug("处理后的参数为:{}", owner);
        log.debug("处理后的参数为:{}", ownerVO);

        //build_unit_rel表业务处理
        log.debug("处理build_unit_rel表业务");
        BuildUnitRelStandardVO buildUnitRelVO = buildUnitRelService(dto.getHouseBuildId(), dto.getHouseUnitId());
        log.debug("处理后的参数为:{}", buildUnitRelVO);

        //house_build表业务处理
        log.debug("处理house_build表业务");
        HouseBuildStandardVO houseBuildStandardVO = buildService(dto.getHouseBuildId());
        log.debug("处理后的参数为:{}", houseBuildStandardVO);

        //house_unit业务处理
        log.debug("处理house_unit表业务");
        HouseUnit houseUnit = houseUnit(dto.getHouseUnitId());
        log.debug("处理后的参数为:{}", houseUnit);


        //house表业务处理
        log.debug("处理house表业务");
        House house = houseService(dto.getRoomNum(), ownerVO.getId(), buildUnitRelVO);
        log.debug("处理后的参数为:{}", house);


        //carportArea表处理业务
        log.debug("处理carportArea表业务");
        //车库不是必填,若车库相关信息一项为null时以下代码不执行
        if (dto.getCarPartNumber() == null || dto.getCarPartAreaId() == null) {
            return;
        }

        CarportAreaStandardVO carportAreaStandardVO = carportAreaService(dto.getCarPartAreaId());
        log.debug("处理后的参数为:{}", carportAreaStandardVO);
        //carport表处理业务
        log.debug("处理carport表业务");
        Carport carport = carportService(carportAreaStandardVO.getId(), dto.getCarPartNumber(), ownerVO.getId());
        log.debug("处理后的参数为:{}", carport);

    }


    /**
     * owner表业务处理
     */
    public Owner ownerService(AddNewOwnerModuleDTO dto) {
        log.debug("处理owner表业务");
        Integer integer = ownerMapper.countByIdCard(dto.getOwnerIdCard());
        if (integer > 0) {
            String message = "添加业主失败,该业主已存在";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        //142703200111010639
        //获取当年年份
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);

        String idCrad = dto.getOwnerIdCard();
        //获取用户出生年
        int ownerBirthYear = Integer.parseInt(idCrad.substring(OWNER_BIRTH_YEAR_FIRST, OWNER_BIRTH_YEAR_END));
        Integer age = year - ownerBirthYear;
        if (age <= 0) {
            String message = "身份证信息输入有误";
            throw new ServiceException(ServiceCode.ERROR_BAD_REQUEST, message);
        }
        //判断手机号是否被使用
        Integer countPhone = ownerMapper.countByPhone(dto.getOwnerPhone());
        if (countPhone > 0) {
            String message = "该手机号已经被注册";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        Owner owner = new Owner();
        owner.setName(dto.getOwnerName());
        owner.setGender(dto.getOwnerGender());
        owner.setIdCard(idCrad);
        owner.setAge(age.toString());
        owner.setPhone(dto.getOwnerPhone());
        owner.setUsername(OWNER_USERNAME_FIR + dto.getOwnerPhone());
        //业主身份证号的后四位
        String idCardLast = idCrad.substring(idCrad.length() - SUBSTRING_LAST_INDEX);
        //业主手机号的后四位
        String phoneLast = dto.getOwnerPhone().substring(dto.getOwnerPhone().length() - SUBSTRING_LAST_INDEX);
        //业主的默认密码为身份证后四位+手机号后四位
        owner.setPassword(idCardLast + phoneLast);
        //默认启用
        owner.setEnable(ENABLE_STATE);
        //业主默认角色
        owner.setRoleId(OWNER_ROLE_ID);
        //业主默认头像
        owner.setAvatar("https://imgavater.ui.cn/avatar/big.png");
        ownerMapper.insert(owner);
        return owner;
    }

    public BuildUnitRelStandardVO buildUnitRelService(Long buildId, Long unitId) {
        //判断单元和栋是否存在
        BuildUnitRelStandardVO vo = buildUnitRelMapper.getStandardByBuildIdAndUnitId(buildId, unitId);
        if (vo == null) {
            String message = "区域(栋)或单元不存在";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        //若存在返回 关联信息
        return vo;
    }

    public HouseBuildStandardVO buildService(Long buildId) {
        //判断栋是否存在
        HouseBuildStandardVO buildVO = houseBuildMapper.getHouseBuildById(buildId);
        if (buildVO == null) {
            String message = "区域(栋)不存在";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        //若存在 返回该栋信息
        return buildVO;
    }

    public HouseUnit houseUnit(Long unitId) {
        HouseUnit houseUnit = houseUnitMapper.getHouseUnitById(unitId);
        if (houseUnit == null) {
            String message = "该单元不存在";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return houseUnit;
    }

    public House houseService(String roomNum, Long ownerId, BuildUnitRelStandardVO buildUnitRelVO) {
        //判断房间号是否存在
        int roomNumCount = houseMapper.countByRoomNum(roomNum);
        if (roomNumCount == 0) {
            String message = "该房间号不存在";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        //判断房产是否已被使用
        HouseStandardVO houseInfo = houseMapper.getHouseByRoomNumAndBuildUnitRelId(roomNum, buildUnitRelVO.getId());
        log.debug("houseInfo:{}", houseInfo);
        //房屋状态id不为  1
        if (!houseInfo.getHouseStateId().equals(HOUSE_STATE_IDLE)) {
            String message = "该房间已经使用";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        //若存在
        //根据房间号 和 栋与单元关联表的id 得到要更改的 house 信息
        HouseStandardVO houseVo = houseMapper.getHouseByRoomNumAndBuildUnitRelId(roomNum, buildUnitRelVO.getId());
        House house = new House();
        house.setId(houseVo.getId());
        house.setOwnerId(ownerId);
        house.setHouseStateId(HOUSE_STATE_SOLD);
        houseMapper.update(house);
        return house;
    }

    public CarportAreaStandardVO carportAreaService(Long carportAreaId) {
        CarportAreaStandardVO carportAreaStandardVO = carportAreaMapper.getStandardById(carportAreaId);
        if (carportAreaStandardVO == null) {
            String message = "该车位区号不存在";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return carportAreaStandardVO;
    }

    public Carport carportService(Long areaId, String number, Long ownerId) {
        Integer countCarport = carportMapper.countCarportByAreaId(areaId);
        if (countCarport == 0) {
            String message = "车位区号不存在";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        Carport carport = carportMapper.getCarportByNumber(number);
        if (carport == null) {
            String message = "该车位编号不存在";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        if (carport.getOwnerId() != null) {
            String message = "该车位已被使用";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        carport.setOwnerId(ownerId);
        carportMapper.update(carport);
        return carport;
    }
}
